import {
    reqOrgList,
} from '@api/api.js';

export default {
    data() {
        return {
            //实现时间限制选择在一个月内而且不能超过当前时间
            pickerOptions: {
                onPick: ({ maxDate, minDate }) => {
                    this.cuttentTime = minDate.getTime();
                    if (maxDate) {
                        this.cuttentTime = "";
                    }
                },
                disabledDate: (time) => {
                    //获取当前时间戳
                    let myDate = new Date();
                    let newY = myDate.getFullYear();
                    let newM = myDate.getMonth() + 1;
                    let newD = myDate.getDate();
                    let oldTime =
                        newY +
                        "-" +
                        (newM < 10 ? "0" + newM : newM) +
                        "-" +
                        (newD < 10 ? "0" + newD : newD) +
                        " " +
                        "23:59:59";
                    // let oldTime="2022-7-5 23:59:59"
                    oldTime = new Date(oldTime).getTime(); // 将中国时间转换成毫秒
                    // 今天之后的时间不可选
                    if (time.getTime() > oldTime) {
                        //time.getTime() > Date.now()
                        return true;
                    }
                    //选择日期范围为一个月
                    if (this.cuttentTime != "") {
                        const one = 30 * 24 * 3600 * 1000;
                        const minTime = this.cuttentTime - one;
                        const maxTime = this.cuttentTime + one;
                        return time.getTime() < minTime || time.getTime() > maxTime;
                    }
                },
            },
            statusList: [{
                    id: 1,
                    name: '未处理',
                },
                {
                    id: 3,
                    name: '修复中',
                },
                {
                    id: 4,
                    name: '已修复',
                },
                {
                    id: 2,
                    name: '已忽略',
                },
            ],
            sourceList: [{
                    id: 1,
                    name: '人工上报',
                },
                {
                    id: 0,
                    name: '设备上报',
                },
            ],
            search_val: {
                imei: '',
                orgId: '',
                status: '',
                type: "",
                source: "",
            },
            orgList: [], //单位数据
            loading: false,
            sysInstanceIndex: 1,
            sysInstanceTotalPages: 10,
            flag: true,
            orgName: '',
            selectedValue: "",
            hasSearch: false,
        };
    },
    components: {},
    mounted() {},
    methods: {
        // 搜索
        async searchClick1() {
            this.page = 1;
            this.limit = 10;
            this.showPopover = false;
            await this.getDataList();
        },
        // 高级搜索重置
        async resetClick1() {
            this.timeArr = []
            this.search_val.orgId = ""
            this.search_val.status = ""
            this.search_val.type = ""
            this.search_val.source = ""
            this.targetId = this.spacesManage.organizationInfo.orgId;
            if (this.$refs.refTreeSearch) {
                this.$refs.refTreeSearch.onclose()
            }
            this.parentId = ''

            this.page = 1;
            this.limit = 10;
            await this.getDataList();
            this.orgName = '';
            this.selectedValue = '';
            this.sysInstanceIndex = 1;
            this.sysInstanceTotalPages = 10;
            if (this.user.roleName != 'INSTERALL_USER' && this.user.roleName != 'UNIT_ADMIN') {
                this.orgList = []
            }
            // this.handleClear()
        },
        // 清空
        handleClear() {
            this.orgName = '';
            this.selectedValue = '';
            this.sysInstanceIndex = 1;
            this.sysInstanceTotalPages = 10;
            this.orgList = []
            this.getOrgList()
        },
        // 远程搜索
        async remoteMethod(query) {
            this.selectedValue = query //保留输入的值
            this.orgName = query;
            this.sysInstanceIndex = 1;
            this.sysInstanceTotalPages = 10;
            this.orgList = []
            this.getOrgList()
        },
        // 选择单位
        changeOrgId(instanceId) {
            this.parentId = instanceId.id
            this.handleClear();
        },
        // 获取单位列表
        async getOrgList() {
            var res = await reqOrgList({
                userId: this.user.userId,
                roleName: this.user.roleName,
                tenantId: this.user.tenantId,
                orgName: this.orgName,
                parentId: this.parentId,
                type: "4"
            });
            if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
            this.orgList = res.data.rows
        },
        async loadMore() {
            if (this.sysInstanceIndex >= Math.ceil(this.sysInstanceTotalPages / 10)) return
            this.sysInstanceIndex++
                this.loading = true;
            // 获取列表数据的方法
            await this.getOrgList()
            this.loading = false;
        },
    },
};